﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace LogUtilHelpV2
{
    public enum LogType
    {
        Trace,
        Warning,
        Error,
        SQL
    }
    public class LogToolV2
    {
        private static string logPath = string.Empty;
        private static string logName = string.Empty;

        public static string LogName
        {
            get
            {
                if (string.IsNullOrEmpty(logName))
                    logName = DateTime.Now.ToString("yyyyMMdd");
                return logName;
            }
            set
            {
                logName = value;
            }
        }
        /// <summary>
        /// 保存日志的文件夹
        /// </summary>
        public static string LogPath
        {
            get
            {
                if (string.IsNullOrEmpty(logPath))
                {
                    logPath = AppDomain.CurrentDomain.BaseDirectory + @"log\";
                    if (!Directory.Exists(logPath))
                        Directory.CreateDirectory(logPath);
                }
                return logPath;
            }
            set
            {
                logPath = value;
            }
        }
        /// <summary>
        /// 写日志
        /// </summary>
        public static void WriteLog(string logType, string msg)
        {
            try
            {

                using (StreamWriter _sw = File.AppendText(LogPath + LogName + "_" + logType + ".Log"))
                    _sw.WriteLine(msg);
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("WriteLog Exception:{0}", ex.Message.Trim()));
            }
        }
        /// <summary>
        /// 写日志
        /// </summary>
        public static void WriteLog(LogType logFile, string msg)
        {
            WriteLog(logFile.ToString(), msg);
        }
    }
}
